package com.webemail.views.controller;

import com.webemail.sys.model.SysEmailAccount;
import com.webemail.sys.model.SysFolder;
import com.webemail.sys.model.SysLabel;
import com.webemail.sys.model.SysMessageSend;
import com.webemail.sys.service.EmailService;
import com.webemail.sys.service.FolderService;
import com.webemail.sys.service.LabelService;
import com.webemail.sys.service.MessageSendService;
import com.webemail.sys.util.CommonUtils;
import com.webemail.sys.util.MailConnect;
import com.webemail.sys.util.MailConnect.FolderType;
import com.webemail.sys.util.MailUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LaiHQ
 */
@Controller
@RequestMapping("/send")
public class EmailSendControllerView {
	@Resource
	private EmailService emailService;
	@Resource
	private FolderService folderService;
	@Resource
	private LabelService labelService;
	@Resource
	private MessageSendService messageSendService;

	private int getUserId() {
		return 1;
	}

	@RequestMapping("/all")
	public String all(HttpServletRequest request, Model model) {
		long userId = this.getUserId();
		List<SysLabel> labelList = new ArrayList<SysLabel>();
		List<SysFolder> folderList = new ArrayList<SysFolder>();
		try {
			labelList = labelService.list(userId);
			folderList = folderService.list(userId);
		} catch (Exception e) {
			e.printStackTrace();
			model.addAttribute("msg", e.getMessage());
			return "mail/error";
		}
		model.addAttribute("labelList", labelList);
		model.addAttribute("folderList", folderList);
		return "mail/message_send";
	}

	@RequestMapping("/detail")
	public String emailDetail(HttpServletRequest request, Model model) {
		String id = null;
		try {
			id = CommonUtils.getString(request.getParameter("id"));
			int idTemp = Integer.parseInt(id);
			SysMessageSend result = messageSendService.get(idTemp, this.getUserId());
			model.addAttribute("message", result);
			model.addAttribute("mail_type", "Send");
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			this.readMessage(id, 1, FolderType.SentFolder);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "mail/email_detail_send";
	}

	/**
	 * 
	 * @param messageIds
	 * @param read
	 * @param folderType
	 * @return
	 */
	private boolean readMessage(String messageIds, int read, FolderType folderType) {
		long userId = this.getUserId();
		String messageId[] = messageIds.split(",");
		SysEmailAccount sysEmailAccount = null;
		MailConnect mailConnect = null;
		int count = 0;
		for (int i = 0; i < messageId.length; i++) {
			long tempId = Long.parseLong(messageId[i]);
			SysMessageSend message = messageSendService.get(tempId, userId);
			if (read == 0) {
				message.setHasRead("false");
			} else {
				message.setHasRead("true");
			}
			int result = messageSendService.update(message);
			if (sysEmailAccount == null) {
				long mailId = message.getMailId();
				sysEmailAccount = emailService.get(mailId, userId);
				mailConnect = new MailConnect(MailUtils.getReMailBean(sysEmailAccount));
				mailConnect.init();
				if (mailConnect.getState() != 0) {
					sysEmailAccount.setIsSubmit("false");
				}
			}
			if (sysEmailAccount != null) {
				if (sysEmailAccount.isSubmit() && !message.isLocalSource()) {
					if (read == 0) {
						mailConnect.unReadMessage(folderType, message.getMessageId(), message.getSentDateTime());
					} else {
						mailConnect.readMessage(folderType, message.getMessageId(), message.getSentDateTime());
					}
				}
			}
			if (result > 0) {
				count++;
			}
		}
		if (mailConnect != null) {
			mailConnect.stop();
		}
		if (count == messageId.length) {
			return true;
		} else {
			return false;
		}
	}
}
